/*
 * Copyright (C) 2020  FunGuide, Inc. All Rights Reserved.
 */


package com.taiji.getway;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Simple to Introduction
 * className: KeyResolverConfiguration
 *
 * @author yanxw
 * @version 2020/7/20 14:26
 */

@Configuration
public class KeyResolverConfiguration {

    /**
     * 请求路径的限流规则
     * @return
     */
//    @Bean
    public KeyResolver pathKeyResolver() {
        return new KeyResolver(){
            public Mono<String> resolve(ServerWebExchange exchange) {
                return Mono.just(exchange.getRequest().getPath().toString());
            }
        };
    }


    /**
     * 请求参数限流
     * @return
     */
    @Bean
    public KeyResolver userKeyResolver() {
        return exchange -> Mono.just(
                exchange.getRequest().getQueryParams().getFirst("userId")
                //exchange.getRequest().getHeaders().getFirst("X-Forwarded-For") 基于请求ip的限流
        );
    }

}

